﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GoDinner.Models
{
    public class Cart
    {
        private List<CartLine> lines = new List<CartLine>();

        public void AddGerecht(Gerecht g, int quantity)
        {
            var line = lines.FirstOrDefault(p => p.Gerecht.Id == g.Id);
            if (line == null)
            {
                lines.Add(new CartLine { Gerecht = g, Quantity = quantity });
            }
            else
            {
                line.Quantity += quantity;
            }
  
        }

        public void RemoveGerecht(Gerecht g)
        {
            lines.RemoveAll(p => p.Gerecht.Id == g.Id);
        }

        public void DecreaseQuantity(Gerecht g)
        {
            var line = lines.FirstOrDefault(p => p.Gerecht.Id == g.Id);
            if (line != null)
            {
                line.Quantity--;
            }

            // Als hoeveelheid 0 is, gerecht verwijderen
            if (line.Quantity == 0)
            {
                RemoveGerecht(g);
            }
        }

        public List<CartLine> GetCartLines()
        {
            return lines;
        }

        public double GetTotal()
        {
            return lines.Sum(l => l.Gerecht.Prijs * l.Quantity);
        }

        public void Clear()
        {
            lines.Clear();
        }
    }
}